﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using TakeoutMangesBLL;
using Model;

namespace TakeoutMangesWeb
{
    public partial class ShopCar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Usersession"] != null)
            {
                logininfo.Attributes.Add("Style", "display:none");
                showinfo.Attributes.Add("Style", "display:block");
                Models.Usersession us = Session["Usersession"] as Models.Usersession;
                user.InnerHtml = us.UserName;
            }
            else
            {
                logininfo.Attributes.Add("Style", "display:block");
                showinfo.Attributes.Add("Style", "display:none");
            }
            if (!IsPostBack) 
            {
                Getshop();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        public void Getshop() 
        {
            Models.Usersession u = Session["Usersession"] as Models.Usersession;
            int id = Convert.ToInt32(u.UserID);
            RepeaterCar.DataSource = ShopcarBLL.GetShopcar(id);
            RepeaterCar.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["Usersession"] = null;
            Session.Remove("Usersession");
        }

        protected void RepeaterCar_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                Model.Shopcar s = new Model.Shopcar();
                Models.Usersession u = Session["Usersession"] as Models.Usersession;
                s.UserID = u.UserID;
                s.ProductID =Convert.ToInt32(e.CommandArgument.ToString());
                int r = ShopcarBLL.deletecarshow(s);
                if (r > 0)
                {
                    Getshop();
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "", "<Script>alert('发生错误，请重试')</Script>");
                }
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            List<int> listid = new List<int>();
            List<int> listprice = new List<int>();
            int a=0;
            for (int i = 0; i < RepeaterCar.Items.Count; i++)
            {
                CheckBox check = (CheckBox)RepeaterCar.Items[i].FindControl("CheckBox1");
                HiddenField hidden = (HiddenField)RepeaterCar.Items[i].FindControl("HiddenField1");
                HiddenField price = (HiddenField)RepeaterCar.Items[i].FindControl("HiddenField2");
                Model.Orders OS = new Model.Orders();
                if (check.Checked)
                {
                    listid.Add(Convert.ToInt32(hidden.Value));
                    listprice.Add(Convert.ToInt32(price.Value));
                    OS.ProductID = (Convert.ToInt32(hidden.Value));
                    OS.Isdelete = 0;
                    int r=OrdersBLL.InsertOrders(OS, out a);
                    if (r > 0) 
                    {

                    }
                    else 
                    {
                        ClientScript.RegisterStartupScript(GetType(), "", "<Script>alert('请勾选想要购买的商品后再付款。')</Script>");
                    }
                }
            }
            Model.OrderInfo OI = new OrderInfo();
            Models.Usersession us = Session["Usersession"] as Models.Usersession;
            OI.UserID = us.UserID;
            OI.OrderInfoID = a;
            OI.OrderState = 1;
            OI.OrderDate = DateTime.Now;
            OI.Isdelete = 0;
            OI.OrderState = 1;
            if (txtRemark.Value != "")
            {
                OI.Remark = txtRemark.Value.Trim();
            }
            else
            {
                OI.Remark = "";
            }
            OrderInfoBLL.InsertOrderInfo(OI);
            int zj = 0;
            for (int i = 0; i < listprice.Count; i++)
            {
                zj += listprice[i];
            }
            if (zj != 0)
            {
                Response.Redirect("Order.aspx?zj=" + zj);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<Script>alert('请选择想要购买的商品后再付款。')</Script>");
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            List<int> listid = new List<int>();
            List<int> listprice = new List<int>();
            for (int i = 0; i < RepeaterCar.Items.Count; i++)
            {
                CheckBox check = (CheckBox)RepeaterCar.Items[i].FindControl("CheckBox1");
                HiddenField price = (HiddenField)RepeaterCar.Items[i].FindControl("HiddenField2");
                if (check.Checked)
                {
                    listprice.Add(Convert.ToInt32(price.Value));
                }
            }
            int zj = 0;
            for (int i = 0; i < listprice.Count; i++)
            {
                zj += listprice[i];
            }
            Label1.Text = zj.ToString();
        }
    }
}